<?php

namespace App\Model\form;

use App\Model\MessageTalk;

trait MessageTalkTrait
{

    /**
     *获取运营号账号列表
     * @param object $where
     * @param array $params
     * @return array
     */
    public function getMessageFriendPage(object $where, $where1, array $params): array
    {
        $query = MessageTalk::where($where)->orWhere($where1)
            ->with(['Friend' => function ($query) {
                $query->select('id', 'nickname', 'bookname', 'path', 'userId', 'account_id');
            }]);
        $count = $query->count();
        $list = $query->offset($params['pageNum'] * $params['pageSize'])
            ->limit($params['pageSize'])->orderByDesc('id')->get()->toArray();
        return ['count' => $count, 'list' => $list];
    }


    /**
     * 获取消息数量
     * @param  $where
     * @return int
     */
    public function getMessageTalkCount($where): int
    {
        return MessageTalk::where($where)->count();
    }


    /**
     * 获取最新一条消息
     * @param  $where
     */
    public function getLastOneMessage($where)
    {
        return MessageTalk::where($where)->orderByDesc('id')->first();
    }

    /**
     * 则查询最后开始有几张图片
     * @param  $where
     */
    public function getLastImageCount($where)
    {
        return MessageTalk::where($where)->orderByDesc('id')->count();
    }

    /**
     * 获取消息详情
     * @param  $where
     */
    public function getMessageInfo($where)
    {
        return MessageTalk::where($where)->first();
    }

    /**
     * 获取消息列表
     * @param  $where
     */
    public function getMessageAllList($where): array
    {
        return MessageTalk::where($where)->orderBy('created_at')->get()->toArray();
    }


    /**
     * 更新消息
     * @param  $where
     * @param $data
     * @return int
     */
    public function updateAllUserMessage($where, $data): int
    {
        return MessageTalk::where($where)->update($data);
    }


}